﻿using Mall.Infrastructure.Model;
using Mall.Infrastructure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sx.Mall.Domain
{
   public partial class ProductBigtype
    {
        /// <summary>
        /// 添加产品大分类
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
       public static CommonResult Add(IRepository<ProductBigtype> repository, ProductBigtype entity)
        {
            CommonResult result = new CommonResult();
            repository.Add(entity);
            result.IsResult = true;
            result.Message = "添加成功";
            return result;
        }

       /// <summary>
       /// 修改分类
       /// </summary>
       /// <param name="repository"></param>
       /// <param name="entity"></param>
       /// <returns></returns>
       public static CommonResult Update(IRepository<ProductBigtype> repository,ProductBigtype entity)
       {
           CommonResult result = new CommonResult();
           var item = repository.Where(m => m.bigtypeId == entity.bigtypeId).FirstOrDefault();

           if (item != null && item.bigtypeId != entity.bigtypeId)
           {
               result.IsResult = false;
               result.Message = "名称已经存在";
           }
           else
           {
               item = repository.Where(m => m.bigtypeId == entity.bigtypeId).First();
               item.CategoryCode = entity.CategoryCode;
               item.Name = entity.Name;
               item.Status = entity.Status;
               result.IsResult = true;
               result.Message = "更新成功";
           }
           return result;
       }


       /// <summary>
       /// 删除大分类
       /// </summary>
       /// <param name="ids"></param>
       /// <returns></returns>
       public static CommonResult Delete(IRepository<ProductBigtype> repository, IRepository<ProductSmallType> smallRepository, CommonOperate<List<int>> option)
       {
           CommonResult result = new CommonResult();

           var parentid = repository.Where(m => option.Option.Contains(m.bigtypeId)).Select(m => m.bigtypeId);
           var item = smallRepository.Where(m => parentid.Contains(m.bigTypeId.Value)).FirstOrDefault();
           if (item == null)
           {
               repository.Update(m => new ProductBigtype
               {
                   IsDelete=true,
               }, m => option.Option.Contains(m.bigtypeId));
               result.IsResult = true;
               result.Message = "删除成功";
           }
           else
           {

               result.IsResult = false;
               result.Message = "该分类下还有小分类，不能删除";
           }
           return result;

       }
    }
}
